<?php

namespace App\Http\Controllers\v1;

use App\Craftsman;
use App\Item;
use App\Member;
use App\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CapintroController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $data = Project::limit(4)->orderBy('sort','ASC')->offset(($request->get('page',1)-1)*3)->get()->each(function (&$item){
            $item['child'] = Craftsman::where('project_id',$item->id)->limit(4)->orderBy('created_at','ASC')->get();
        });
        return $this->success($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        return $this->success(Craftsman::with(['teams','header'])->findOrFail($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function member($id)
    {
        return $this->success(Member::whereCraftmans($id)->get());
    }

    public function item($id)
    {
        return $this->success(Item::whereCraftmansId($id)->get());
    }

    public function more($id,Request $request){
        $page = $request->get('page',1);
        $size = $request->get('size',16);
        return $this->page(Craftsman::whereProjectId($id)->limit($size)->offset(($page-1)*$size)->get(),Craftsman::whereProjectId($id)->count());
    }
}
